package com.example.helloworld

import android.app.ListActivity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import android.widget.Toast.makeText as toastMakeText
import java.util.*

class MainActivity : AppCompatActivity() {
    val TAG = "@@MainActivity"
    //override

    private var username:String? = null
    private var pass:String? = null

    override fun onCreate(savedInstanceState:Bundle?) {
        super.onCreate(savedInstanceState)
        println("onCreate")
        Log.d(TAG,"onCreate")
        setContentView(R.layout.activity_main)

        val iptUsername=findViewById<EditText>(R.id.ipt_username)
        val iptpass=findViewById<EditText>(R.id.ipt_pass)
        val userNameWacher = object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"beforeTextChanged:$p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"onTextChanged:$p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG,"afterTextChanged:$p0")
            }

        }
        val userpassWacher = object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"beforeTextChanged:$p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"onTextChanged:$p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG,"afterTextChanged:$p0")
            }
        }
        iptUsername.addTextChangedListener(userNameWacher)
        iptpass.addTextChangedListener(userpassWacher)
    }



    override fun onStart(){
       super.onStart()
        println("onStart")
        Log.d(TAG,"onDestory")
    }
    override fun onPause(){
        super.onPause()

        println("onPause")
        Log.d(TAG,"onPause")
    }
    override fun onRestart(){
        super.onRestart()
        println("onRestart")
        Log.d(TAG,"onRestart")
    }
    override fun onResume(){
        super.onResume()
        println("onResume")
        Log.d(TAG,"onResume")
    }
    override fun onStop(){
        super.onStop()
        println("onStop")
        Log.d(TAG,"onStop")
    }
    override fun onDestroy(){
        super.onDestroy()
        println("onDestroy")
        Log.d(TAG,"onDestory")
    }
    fun onClick(view:View){
      Log.i(TAG,"onClick ${view.id}")

      when(view.id){
            R.id.activitys->{
                Log.d(TAG,"username=$username")
                Log.d(TAG,"userpass=$pass")

                val intent = Intent(this,SecondActivity::class.java)
                AlertDialog.Builder(this)
                    .setTitle("请确认").setMessage("确定要登陆吗")
                    .setNegativeButton("取消"){d,w->
                        Toast.makeText(this,"已取消",Toast.LENGTH_LONG).show()
                    }
                    .setPositiveButton("确定"){d,w->tologin(intent)}
                    .show()

        }
          R.id.activityt->{
              val intent = Intent(this,ThirdActivity::class.java)
              startActivity(intent)
          }

     }
    }

    private fun tologin(intent:Intent){
        val process = findViewById<ProgressBar>(R.id.progress_enter)
        process.visibility = View.VISIBLE
        process.progress = 0
        // TODO: 请求网络 耗时
        val timer = Timer()
       // val context = this
        val task = object :TimerTask(){
            override fun run() {
                Log.d(TAG,"progress = ${process.progress}")
                process.progress = process.progress + 1
                if(process.progress == 10){
                    timer.cancel()
                   Handler(Looper.getMainLooper()).post {
                        process.visibility = View.GONE
                        startActivity(intent)
                   }
                }
            }
        }
        timer.schedule(task,0,100)
    }


    }



